package com.sskj.xgja.verify;


import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.MVPActivity;
import com.sskj.xgja.R;
import com.sskj.xgja.view.SelectPictureDialog;

import java.io.File;
import java.util.List;

import butterknife.BindView;

/**
 * 实名认证 第一步
 *
 * @author Hey
 * Create at  2019/03/20
 */
public class Verify1Activity extends MVPActivity<Verify1Presenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_idCard)
    EditText editIdCard;
    @BindView(R.id.edit_bank_name)
    EditText editBankName;
    @BindView(R.id.edit_bank_number)
    EditText editBankNumber;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.edit_bank_province)
    EditText editBankProvince;
    @BindView(R.id.edit_bank_city)
    EditText editBankCity;
    @BindView(R.id.edit_bank_address)
    EditText editBankAddress;

    @BindView(R.id.frontImage)
    ImageView frontImage;
    @BindView(R.id.backImage)
    ImageView backImage;


    private SelectPictureDialog selectPictureDialog;

    private final int SELECT_FRONT = 1000;
    private final int SELECT_BACK = 1001;

    private String frontImagePath;
    private String backImagePath;



    @BindView(R.id.cardImage)
    ImageView cardImage;


    private final int SELECT_CARD = 1003;
    private String cardImagePath;

    @Override
    public int getLayoutId() {
        return R.layout.activity_verify1;
    }

    @Override
    public Verify1Presenter getPresenter() {
        return new Verify1Presenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(v -> finish());
        ClickUtil.click(frontImage, view -> {
            selectPic(SELECT_FRONT);
        });
        ClickUtil.click(backImage, view -> {
            selectPic(SELECT_BACK);
        });
        ClickUtil.click(cardImage, view -> selectPic(SELECT_CARD));

    }

    public void selectPic(int requestCode) {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(Verify1Activity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(requestCode);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(Verify1Activity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(requestCode);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_FRONT || requestCode == SELECT_BACK) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(0);
                    String path;
                    if (media.isCompressed()) {
                        path = media.getCompressPath();
                    } else {
                        path = media.getPath();
                    }
                    if (requestCode == SELECT_FRONT) {
                        frontImagePath = path;
                        frontImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        frontImage.setPadding(0, 0, 0, 0);
                        Glide.with(this).load(path).into(frontImage);
                    } else {
                        backImagePath = path;
                        backImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        backImage.setPadding(0, 0, 0, 0);
                        Glide.with(this).load(path).into(backImage);
                    }
                }
            }
            if (requestCode == SELECT_CARD) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(0);
                    String path;
                    if (media.isCompressed()) {
                        path = media.getCompressPath();
                    } else {
                        path = media.getPath();
                    }
                    cardImagePath = path;
                    cardImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    cardImage.setPadding(0, 0, 0, 0);
                    Glide.with(this).load(path).into(cardImage);
                }
            }
        }


    }

    @Override
    public void initData() {
        ClickUtil.click(btnNext, view -> {
//            if (isEmpty(editName)) {
//                ToastUtils.show("请输入姓名");
//                return;
//            }
//            if (isEmpty(editIdCard)) {
//                ToastUtils.show("请输入身份证号");
//                return;
//            }

            if (isEmpty(editBankName)) {
                ToastUtils.show("请输入银行卡类型");
                return;
            }
//            if (isEmpty(editBankProvince)) {
//                ToastUtils.show("请输入开户行省份");
//                return;
//            }
//
            if (isEmpty(editBankAddress)) {
                ToastUtils.show("请输入开户行地址");
                return;
            }


            if (isEmpty(editBankNumber)) {
                ToastUtils.show("请输入银行卡号");
                return;
            }
            if (getText(editBankNumber).length() <= 14) {
                ToastUtils.show("请输入正确的银行卡号");
                return;
            }
            if (TextUtils.isEmpty(frontImagePath)) {
                ToastUtils.show("请选择身份证正面照片");
                return;
            }
            if (TextUtils.isEmpty(backImagePath)) {
                ToastUtils.show("请选择身份证反面照片");
                return;
            }
            if (TextUtils.isEmpty(cardImagePath)) {
                ToastUtils.show("请选择银行卡正面照片");
                return;
            }
            mPresenter.verify3(getText(editBankName), getText(editBankNumber), getText(editBankAddress), new File(frontImagePath), new File(backImagePath), new File(cardImagePath));

//            Verify2Activity.start(this, getText(editBankName), getText(editBankNumber),getText(editBankAddress));
//            mPresenter.verify1(account, getText(editName), getText(editIdCard), getText(editBankName), getText(editBankNumber),getText(editBankProvince),getText(editBankCity),getText(editBankAddress));
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, Verify1Activity.class);
        context.startActivity(intent);
    }


    public void submitSuccess() {
//        Verify2Activity.start(this);
//        finish();
    }

    public void verify3Success() {
        finish();
    }
}
